/**
 * 权限生成路由模块
 */
import { asyncRoutes, constantRoutes, errorRoutes } from "@/router/index";
import Layout from '@/layout'
import { getMenuById } from '@/api/role'


/**
 * 动态路由配置，需要权限
 */
const asnycRouteMeun = {
    // "/dashboard": () => import('@/views/dashboard/index'),
    "user_list": () => import('@/views/user_list/index'),
    "reard_list": () => import('@/views/reard_list/index'),
    "role_list": () => import('@/views/role_list/index'),
    "book_list": () => import('@/views/book_list/index'),
    "book_info":  () => import('@/views/book_list/info'),
    "book_cate": () => import('@/views/book_cate/index'),
    "book_comment": () => import('@/views/book_comment/index'),
    "comment_info": () => import('@/views/book_comment/info'),
    "loss_list": () => import('@/views/loss_list/index'),

    "now_book": () => import('@/views/now_book/index'),
    "now_info": () => import('@/views/now_book/info'),
    "history_book": () => import('@/views/history_book/index'),
    "his_info": () => import('@/views/history_book/info'),
    "expire_book": () => import('@/views/expire_book/index'),
    "expire_info": () => import('@/views/expire_book/info'),
    "public_list": () => import('@/views/public_list/index'),
    "indemnify_list": () => import('@/views/indemnify_list/index'),
}



/**
 * 判断路由路径是否相等
 * @param asyncRouterItem
 * @param userRouteItem
 */
function hasPath(asyncRouterItem, userRouteItem) {
    return asyncRouterItem.path === userRouteItem.path;
}

/**
 * 生成菜单
 * @param routes
 * @param data
 * @returns {*[]}
 */
export function generaMenu(routes, data) {
    console.log(data, "data")
    for (let item of data) {
        let menu
        if (item.children.length == 0) {
            console.log("pppppppp")
            menu = {
                path: '/',
                component: Layout,
                redirect: '/',
                children: [
                    {
                        path: '/dashboard',
                        component: () => import("@/views/dashboard/index"),
                        name: `${item.name}`,
                        meta: { title: `${item.name}`, icon: `${item.icon}` }
                    }
                ]
            }
        } else {
            let menuSub = [];
            for (let itemIn of item.children) {
                let obj = {
                    path: `${itemIn.component}`,
                    component: asnycRouteMeun[`${itemIn.component}`],
                    name: `${itemIn.name}`,
                    meta: { title: `${itemIn.name}`, icon: `${itemIn.icon}` }
                }
                if (itemIn.hidden == 1) {
                    obj.hidden = true;
                }
                menuSub.push(obj)
            }
            menu = {
                path: `${item.component}`,
                component: Layout,
                children: [].concat(menuSub),
                redirect: `${item.component}/${item.children[0].component}`,
                name: `${item.name}`,
                meta: { title: `${item.name}`, icon: `${item.icon}` }
            }
        }
        routes.push(menu)
    }
}


/**
 * 数据
 * @type {{routes: Array, addRoutes: Array}}
 */
const state = {
    hasUserRouter: false,
    routes: [],
    addRoutes: []
}
const mutations = {
    /**
     * 设置路由表
     * @param state 数据
     * @param routes 路由
     * @constructor
     */
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes).concat(errorRoutes)
    },
    /**
     * 设置路由状态
     * @param state
     * @param states
     * @constructor
     */
    SET_ROUTER_STATE: (state, states) => {
        state.hasUserRouter = states;
    }
}
const actions = {
    /**
     * 根据用户路由权限递归路由表
     * @param commit
     * @param userRouterList 用户路由权限表
     * @returns {Promise}
     */
    generateRoutes({ commit }, roleId) {
        return new Promise(resolve => {
            console.log(roleId, "roleId")
            getMenuById(roleId).then(res => {
                let routes = []
                generaMenu(routes, res.data)
                commit('SET_ROUTES', routes)
                commit('SET_ROUTER_STATE', true)
                resolve(routes)
            })
        })
    },
    removeRouterState({ commit }) {
        commit('SET_ROUTER_STATE', false);
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
